<template>
  <div class="add-form">
    <el-form :model="form" ref="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="5">
          <el-form-item label="用户名" style="width:400px" prop="title">
            <el-input v-model="form.title" placeholder="请输入用户名"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8" :offset="5">
          <el-form-item label="权限分配">
            <el-tree
              :default-checked-keys="form.permissions"
              :data="treeList"
              show-checkbox
              :default-expand-all="false"
              node-key="id"
              ref="tree"
              highlight-current
              :props="defaultProps"
            ></el-tree>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row style="transform: translateY(35px);">
        <el-col :span="8" :push="18">
          <el-form-item>
            <el-button @click="$emit('close')">取消</el-button>
            <el-button type="primary" @click="onSubmit">确定</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { detail, update } from '@/api/base/permissions.js'
import { list } from '@/api/base/menus'
export default {
    name: 'PermissionGroupsApiAdd',
    data() {
        return {
            form: {},
            rules: {
                title: [
                    {
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    }
                ]
            },
            defaultProps: {
                children: 'points',
                label: 'title'
            },
            treeList: [] //权限列表
        }
    },
    props: {
        rowId: {
            type: [String, Number],
            required: true
        },
        editDialogFormVisible: {
            type: Boolean
        }
    },
    created() {
        this.getTreeList() //权限列表
        this.getPermissiosList() //d点击id的权限信息
    },
    methods: {
        async getPermissiosList() {
            let { data: res } = await detail({
                id: this.rowId
            })

            this.form = res
        },
        //权限列表数据
        async getTreeList() {
            let { data: res } = await list()
            this.treeList = res
            let obj = [
                {
                    title: '系统菜单和页面权限点',
                    points: JSON.parse(JSON.stringify(this.treeList).replaceAll('childs', 'points'))
                }
            ]
            this.treeList = obj
        },
        async onSubmit() {
            //tree方法 若当前节点被勾选 则被选中的节点会返回一个数组
            let authCheks = [...this.$refs.tree.getCheckedKeys(), ...this.$refs.tree.getHalfCheckedKeys()]

            let { data: res } = await update({
                id: this.rowId,
                title: this.form.title,
                permissions: authCheks
            })
            this.$message.success('修改成功')
            this.getPermissiosList()
            this.$emit('close')
        }
    }
}
</script>

<style>
</style>
